#ifndef _IED_SERVER_H_
#define _IED_SERVER_H_
#include "Util.h"
#include "dataShare/include/ApplicationLayer.h"
#include "iec61850_model.h"

#include <iec61850_server.h>
#include <mms_value.h>

class Server {
  public:
    Server();
    virtual ~Server();

    void Init();

    void Start();

    void Config();

    static void SigintHandler(int sig);

    void Lock();

    void Unlock();

  private:
    static void ConnectionHandler(IedServer        self,
                                  ClientConnection connection,
                                  bool             connected,
                                  void            *parameter);

    void WriteAccessPolicy();

    void SetControlHandler(DataObject *object);

    // NOTE: ControlHandler 接口, 添加DataObject
    void ControlHandler();

    // NOTE: Contrl 数据处理接口
    static ControlHandlerResult ControlHandler(ControlAction action,
                                               void         *parameter,
                                               MmsValue     *value,
                                               bool          test);

    // NOTE: WriteHandler 接口,添加DataAttribute
    void WriteHandler();

    void HandleWriteAccess(DataAttribute *attr);

    // NOTE: Write 数据处理接口 根据Data Attribute的类型，设置对应的转发到对应的CCU内存中去
    static MmsDataAccessError WriteAccessHandler(DataAttribute   *dataAttribute,
                                                 MmsValue        *value,
                                                 ClientConnection connection,
                                                 void            *parameter);

    static void
    goCbEventHandler(MmsGooseControlBlock goCb, int event, void *parameter);

  public:
    static IedServer iedServer;
    static int       running;

  private:
    static std::shared_ptr<ApplicationLayer> appLayer;
};
#endif // !_IED_SERVER_H_
